﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using My.Core.Authorization.Provider;
using My.Framework.AspNetCore;

namespace My.Core.Authorization
{
    /// <summary>
    /// 程序启动器 - 自动化注册服务
    /// </summary>
    public class AuthorizationStartup : StartupModule<AuthorizationStartup>
	{
		/// <summary>
		/// 程序启动器
		/// </summary>
		public AuthorizationStartup() => Order = 1;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="webApplicationBuilder"></param>
		public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
		{
			var services = webApplicationBuilder.Services;

			services.AddAuthorization();
			//services.AddSingleton<IAuthorizationHandler, JwtHandler>();
			//services.AddSingleton<IAuthorizationHandler, PermissionAuthorizationHandler>();
			services.AddSingleton<IAuthorizationPolicyProvider, AuthorizationPolicyProvider>();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="webApplication"></param>
		public override void Configure(WebApplication webApplication)
		{
			//app.UseAuthentication会启用Authentication中间件，该中间件会根据当前Http请求中的Cookie信息来设置HttpContext.User属性（后面会用到），
			//所以只有在app.UseAuthentication方法之后注册的中间件才能够从HttpContext.User中读取到值，
			//这也是为什么上面强调app.UseAuthentication方法一定要放在下面的app.UseMvc方法前面，因为只有这样ASP.NET Core的MVC中间件中才能读取到HttpContext.User的值。

			webApplication.UseAuthorization();
		}
	}
}
